package messages;

import java.util.ArrayList;
import java.util.List;

/**
 * This is a wrapper class for a ArrayList<Byte>
 * Provides some additional convenience methods
 * 
 * @author Ian Doerr
 */
public class ByteList {
    
    public static final byte MAX_LENGTH = 0x7F;
    
    private ArrayList<Byte> data = new ArrayList<Byte>();
    
    /**
     * Creates an empty ByteList
     */
    public ByteList()
    {
        //NOOP
    }
    
    /**
     * Copies the contents of the List to the new ByteList
     * 
     * @param data
     */
    public ByteList(List<Byte> data)
    {
        for (Byte b : data)
        {
            addByte(b);
        }
    }
    
    /**
     * Copies the contents of the byte[] to the new ByteList
     * 
     * @param data
     */
    public ByteList(byte[] data)
    {
        for (byte b : data)
        {
            addByte(b);
        }
    }
    
    /**
     * Adds a byte[] to the list
     * 
     * @param value
     * @return success
     */
    public boolean addByte(byte value)
    {
        if (data.size() < MAX_LENGTH)
        {
            data.add(value);
            return true;
        }
        else
        {
            return false;
        }
    }
    
    /**
     * Returns a byte[] version of the current list
     * 
     * @return byte[]
     */
    public byte[] toByteArray()
    {
        byte[] retVal = new byte[getSize()];
        
        int curIndex = 0;
        
        for (byte b : data)
        {
            retVal[curIndex++] = b;
        }
        
        return retVal;
    }
    
    /**
     * Returns the size of the list 
     * 
     * @return int
     */
    public int getSize()
    {
        return data.size();
    }
    
}
